package com.coscon.service.impl;

import com.coscon.mapper.SysConfigMapper;
import com.coscon.model.auth.LeftMenu;
import com.coscon.model.db.SysConfig;
import com.coscon.service.CoreAuthService;
import com.coscon.shiro.MyShiroFilterFactoryBean;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.filter.mgt.DefaultFilterChainManager;
import org.apache.shiro.web.filter.mgt.PathMatchingFilterChainResolver;
import org.apache.shiro.web.servlet.AbstractShiroFilter;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Slf4j
@Service
public class CoreAuthServiceImpl implements CoreAuthService {

    @Resource
    private SysConfigMapper sysConfigMapper;

    @Override
    public List<LeftMenu> getLeftNavigationMenuListByUserId(String userId, String contextPath) {
        List<LeftMenu> list = new ArrayList<>();
        list.add(new LeftMenu("10008","欢迎主页","fa fa-home",contextPath + "/jm/manager/home/welcomePage",0,false));
        list.add(new LeftMenu("menu_a_00","文件浏览","fa fa-folder",contextPath + "/jm/manager/fileBrower/index",0,false));
        list.add(new LeftMenu("menu_a_01","视频播放控制","fa fa-circle-o",contextPath + "/jm/manager/video/index",0,false));
        list.add(new LeftMenu("menu_a_02","音频播放控制","fa fa-headphones",contextPath + "/jm/manager/music/index",0,false));
        list.add(new LeftMenu("menu_a_03","PPT播放控制","fa fa-desktop",contextPath + "/jm/manager/ppt/index",0,false));
        list.add(new LeftMenu("menu_a_06","模拟键盘","fa fa-keyboard-o",contextPath + "/jm/manager/keyboard/index",0,false));
        list.add(new LeftMenu("menu_a_07","模拟鼠标","fa fa-mouse-pointer",contextPath + "/jm/manager/mouse/index",0,false));
        list.add(new LeftMenu("menu_a_08","系统控制","fa fa-windows",contextPath + "/jm/manager/systemOs/index",0,false));
        list.add( getSytemMenu(contextPath));
        list.add(new LeftMenu("menu_a_10","使用帮助","fa fa-info",contextPath + "/jm/manager/help/index",0,false));
        return list;
    }

    @Override
    public Map<String, String> getShiroFilterMap() {
        Map<String, String> filterMap = new HashMap<String, String>();
        // 登出
        filterMap.put("/logout", "logout");
        //无需登录的资源
        filterMap.put("/login/**", "anon");//登录界面
        filterMap.put("/plug-in/**", "anon");//登录界面引用的静态资源


        filterMap.put("/jm/api/**", "anon");//API暂时不用鉴权


        SysConfig sysConfig = this.sysConfigMapper.getConfigByKey("security_is_user");
        String securityIsUser =null;
        if(sysConfig!=null){
            securityIsUser = sysConfig.getConfigVal();
        }
        log.debug("security_is_user=" + securityIsUser);
        if(StringUtils.equalsIgnoreCase(securityIsUser,"on")){
            // 对所有用户认证
            filterMap.put("/**", "authc");
        }else{
            //不需要认证
            filterMap.put("/**", "anon");
        }
        return filterMap;
    }

    @Resource
    private MyShiroFilterFactoryBean shiroFilterFactoryBean;
    @Override
    public boolean reloadApacheShiroAuthFilterChains() {
        AbstractShiroFilter shiroFilter = null;
        try{
            shiroFilter = (AbstractShiroFilter)shiroFilterFactoryBean.getObject();
        } catch(Exception e) {
            log.error("getShiroFilter from shiroFilterFactoryBean error!", e);
            throw new RuntimeException("get ShiroFilter from shiroFilterFactoryBean error!");
        }
        PathMatchingFilterChainResolver filterChainResolver =(PathMatchingFilterChainResolver)shiroFilter.getFilterChainResolver();
        DefaultFilterChainManager manager =(DefaultFilterChainManager)filterChainResolver.getFilterChainManager();
        //清空老的权限控制
        manager.getFilterChains().clear();
        shiroFilterFactoryBean.getFilterChainDefinitionMap().clear();
        //重新设置规则
        shiroFilterFactoryBean.setFilterChainDefinitionMap(getShiroFilterMap());

        //重新构建生成
        Map<String, String> chains = shiroFilterFactoryBean.getFilterChainDefinitionMap();
        for(Map.Entry<String, String> entry :chains.entrySet()) {
            String url = entry.getKey();
            String chainDefinition =entry.getValue().trim().replace(" ", "");
            manager.createChain(url,chainDefinition);
        }
        log.debug("重新加载URL权限配成成功!");
        return true;
    }


    private LeftMenu getSytemMenu(String contextPath) {
        //报表
        LeftMenu menuLegacy = new LeftMenu("menu_a_09","其他辅助功能","fa fa-paw","",0,false);
//        menuLegacy.getChildren().add(new LeftMenu("menu_a_09_01","看电子书","fa fa-camera", contextPath + "/jm/manager/ebook/index",0,false));
        menuLegacy.getChildren().add(new LeftMenu("menu_a_09_02","画面截屏","fa fa-camera",contextPath + "/jm/manager/screenCapture/index",0,false));
        menuLegacy.getChildren().add(new LeftMenu("menu_a_09_03","参数配置","fa fa-gears",contextPath + "/jm/manager/config/index",0,false));
        menuLegacy.getChildren().add(new LeftMenu("menu_a_09_04","网络唤醒","fa fa-gears",contextPath + "/jm/manager/wakeup/index",0,false));
        menuLegacy.getChildren().add(new LeftMenu("menu_a_09_05","系统进程","fa fa-gears",contextPath + "/jm/manager/process/index",0,false));
        menuLegacy.getChildren().add(new LeftMenu("menu_a_09_06","音频格式转换","fa fa-gears",contextPath + "/jm/manager/audioConversion/index",0,false));
        return menuLegacy;
    }





}
